package com.aivin.lib_reader.tool;

import android.annotation.SuppressLint;
import android.graphics.Canvas;

import com.aivin.lib_base.spiders.ChapterSpider;
import com.aivin.lib_base.spiders.ISpiderCallBack;
import com.aivin.lib_base.spiders.SpiderTool;
import com.aivin.lib_base.tool.RxjavaTool;
import com.aivin.lib_reader.ReaderConfig;
import com.aivin.lib_reader.TurnStatus;
import com.aivin.lib_reader.icallback.IReaderManager;
import com.aivin.lib_reader.view.ReaderView;

import io.reactivex.Observable;

import java.util.List;


public  class ReaderManager  implements IReaderManager {

    private List<ChapterSpider> mChapterList;
    private ReaderView mReaderView;
    private ReaderResolve mReaderResolve;
    private String chapterName ="" ;
    private int tempCharIndex = 0  ;
    private int chapterIndex = 0  ;
    private SpiderTool spiderTool ;
    private ISpiderCallBack iSpiderCallBack ;

    public ReaderManager(SpiderTool spiderTool ,ISpiderCallBack iSpiderCallBack){
        this.spiderTool =spiderTool ;
        this.iSpiderCallBack =iSpiderCallBack;
    }

    @Override
    public final TurnStatus toPrevPage() {
        int pageIndex = mReaderResolve.getPageIndex();
        if (pageIndex > 0) {
            mReaderResolve.setPageIndex(pageIndex - 1);
            mReaderResolve.setCharIndex(mReaderResolve.getCurrPageFirstCharIndex());
            return TurnStatus.LOAD_SUCCESS;
        }
        return toPrevChapter();
    }

    @Override
    public final TurnStatus toPrevChapter() {
        int chapterIndex = mReaderResolve.getChapterIndex();
        if (chapterIndex == 0) {
            return  TurnStatus.NO_PREV_CHAPTER ;
        }
        return toSpecifiedChapter(chapterIndex - 1, ReaderResolve.LAST_INDEX);
    }


    @Override
    public final TurnStatus toNextPage() {
        int pageIndex = mReaderResolve.getPageIndex();
        if (pageIndex < mReaderResolve.getPageSum() - 1) {
            mReaderResolve.setPageIndex(pageIndex + 1);
            mReaderResolve.setCharIndex(mReaderResolve.getCurrPageFirstCharIndex());
            return TurnStatus.LOAD_SUCCESS;
        }
        return toNextChapter();
    }


    /**
     * 跳转到下一章的最后一个
     */
    @Override
    public final TurnStatus toNextChapter() {
        int chapterIndex = mReaderResolve.getChapterIndex();
        if (chapterIndex >= mReaderResolve.getChapterSum() - 1) {
            return  TurnStatus.NO_NEXT_CHAPTER ;
        }
        return toSpecifiedChapter(chapterIndex + 1, ReaderResolve.FIRST_INDEX);
    }


    @Override
    public final TurnStatus toSpecifiedChapter(final int chapterIndex, final int charIndex) {
        if ( getChapterCount() == 0) {
            return TurnStatus.LOAD_FAILURE;
        }

        ChapterSpider chapterItem =  mChapterList.get(chapterIndex);
        this.chapterName = chapterItem.getTitle();
        this.chapterIndex = chapterIndex;
        this.tempCharIndex = charIndex;

        String url = chapterItem.getUrl();


        spiderTool.getPapters(url ,iSpiderCallBack );
        return  TurnStatus.DOWNLOADING ;
    }

    @Override
    public void drawPage( Canvas canvas) {
        mReaderResolve.drawPage(canvas);
    }

    @SuppressLint("CheckResult")
    public void setContent(String content){
        if(content != null  )   {
            Observable.just( content)
                    .compose(RxjavaTool::toSimpleObservable)
                    .subscribe(value ->    {
                        mReaderResolve.setChapterIndex(chapterIndex);
                        mReaderResolve.setCharIndex(tempCharIndex);
                        mReaderResolve.setTitle(chapterName);
                        mReaderResolve.setContent(value);
                        mReaderView.invalidateBothPage();
                    }) ;
        }
    }

   /* private ISpiderCallBack iSpiderCallBack = new ISpiderCallBack() {


        @SuppressLint("CheckResult")
        @Override
        public void onGetContent(String content) {

        }


    };*/

    public void setReaderView(ReaderView readerView,   ReaderConfig readerConfig) {
        mReaderResolve = new ReaderResolve(readerConfig);
        this.mReaderView = readerView;
        initReaderResolve();
    }

    private void initReaderResolve() {
        mReaderResolve.setArea(mReaderView.getMeasuredWidth(), mReaderView.getMeasuredHeight());// initReaderResolve
        mReaderResolve.setChapterSum( getChapterCount());
    }




    /**
     *  阅读器配置
     */
    public void setReaderConfig(ReaderConfig readerConfig) {
        mReaderResolve.setReaderConfig(readerConfig);
    }


    public void setChapterList(List<ChapterSpider> chapters) {
        this.mChapterList = chapters;
    }


    public void onAreaChanged(int width, int height) {
        mReaderResolve.setArea(width, height);
    }





    /**
     *  数据有变化，比如从0到有，需要重绘界面，让数据显示出来 。
     */
    public void notifyDataSetChanged() {
        mReaderResolve.setChapterSum( getChapterCount());
        int chapterIndex =   mReaderResolve.getChapterIndex() <=  getChapterCount() ?  mReaderResolve.getChapterIndex() : 0 ;
        TurnStatus turnStatus = toSpecifiedChapter(chapterIndex, mReaderResolve.getCharIndex());

        if (turnStatus == TurnStatus.LOAD_SUCCESS) {
            mReaderView.invalidateCurrPage();
        }

    }

    private int getChapterCount() {
        return mChapterList == null ? 0 : mChapterList.size();
    }


    public int getCurrentChapterIndex(){
        return  mReaderResolve.getChapterIndex() <=  getChapterCount() ?  mReaderResolve.getChapterIndex() : 0;
    }

    public int geCurrentCharIndex(){
        return  mReaderResolve.getCharIndex() ;
    }

}